﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace MVC3DEMO.Controllers
{
    public class CKEditorImageController : Controller
    {
        MediaController MediaCL = new MediaController();

        public ActionResult GetPictures(string folderImagesPath, string folderThumbsPath, int CKEditorFuncNum)
        {
            ViewData["funcNum"] = CKEditorFuncNum;
            var di = new DirectoryInfo(Server.MapPath(folderImagesPath));
            if (di.Exists)
            {
                IList<string> criterias = new List<string>() { "*.jpeg", "*.jpg", "*.pjpeg", "*.pjpg", "*.gif" };

                var listFileImagePath = (from ext in criterias from file in di.GetFiles(ext) select file.Name).ToList();

                var dt = new DirectoryInfo(Server.MapPath(folderThumbsPath));
                var listFileOfThumb = new List<string>();
                if (dt.Exists)
                {
                    listFileOfThumb = dt.GetFiles().Select(file => file.Name).ToList();
                }
                var listFileThumbPath = new List<string>();
                foreach (string fileName in listFileImagePath)
                {
                    if (listFileOfThumb.Contains(fileName))
                    {
                        listFileThumbPath.Add(folderThumbsPath + "/" + fileName);
                    }
                    else
                    {
                        string path = folderImagesPath + "/" + fileName;
                        // var imageResize = new ImageResizer(Server.MapPath(path));
                        //imageResize.ResizeToJpeg(100, 100, folderThumbsPath + "/" + fileName);
                        listFileThumbPath.Add(path);
                    }
                }

                ViewData["items"] = listFileThumbPath;
                ViewData["messageUpload"] = "";
                ViewData["folderThumbsPath"] = folderThumbsPath;
                ViewData["folderImagesPath"] = folderImagesPath;
                return View();
            }
            ViewData["message"] = "Chưa có hình ảnh nào cho bài viết này! Xin vui lòng sử dụng chức năng upload hình ảnh!";
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult GetPictures(string folderImagesPath, string folderThumbsPath, int CKEditorFuncNum, string fileUpload)
        {
            var pictureUpload = MediaCL.UploadPicture(Request, Server, folderImagesPath, folderThumbsPath, 150, 150);

            ViewData["funcNum"] = CKEditorFuncNum;
            if (pictureUpload.Option != 1 && pictureUpload.Option < 4)
            {
                ViewData["messageUpload"] = pictureUpload.ResultString;
                ViewData["message"] = "";
                return View();
            }
            // string folderImagesPath = "/Content/Images/Press/Images/" + folderName + "/";
            // string folderThumbPath = "/Content/Images/Press/Thumbs/" + folderName + "/";
            var di = new DirectoryInfo(Server.MapPath(folderImagesPath));
            if (di.Exists)
            {
                IList<string> criterias = new List<string>() { "*.jpeg", "*.jpg", "*.pjpeg", "*.pjpg", "*.gif", "*.png" };

                var listFileImagePath = (from ext in criterias from file in di.GetFiles(ext) select file.Name).ToList();

                var dt = new DirectoryInfo(Server.MapPath(folderThumbsPath));
                var listFileOfThumb = new List<string>();
                if (dt.Exists)
                {
                    listFileOfThumb = dt.GetFiles().Select(file => file.Name).ToList();
                }
                var listFileThumbPath = new List<string>();
                foreach (string fileName in listFileImagePath)
                {
                    if (listFileOfThumb.Contains(fileName))
                    {
                        listFileThumbPath.Add(folderThumbsPath + "/" + fileName);
                    }
                    else
                    {
                        string path = folderImagesPath + "/" + fileName;
                        // var imageResize = new ImageResizer(Server.MapPath(path));
                        // imageResize.ResizeToJpeg(100, 100, Server.MapPath(folderThumbsPath + "/" + fileName));
                        listFileThumbPath.Add(path);
                    }
                }

                ViewData["items"] = listFileThumbPath;
                ViewData["folderThumbsPath"] = folderThumbsPath;
                ViewData["folderImagesPath"] = folderImagesPath;
                return RedirectToAction("GetPictures", new { folderImagesPath = folderImagesPath, folderThumbsPath = folderThumbsPath, CKEditorFuncNum = CKEditorFuncNum });
            }
            ViewData["message"] = "Chưa có hình ảnh nào cho bài viết này! Xin vui lòng sử dụng chức năng upload hình ảnh!";
            return View();
        }


    }
}
